package com.lib.util;

import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Point;
import android.provider.Settings;
import androidx.core.content.PermissionChecker;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.qbw.log.XLog;

import java.io.File;
import java.io.FileFilter;
import java.util.regex.Pattern;

/**
 * @author qbw
 * @createtime 2016/04/15 11:26
 * 获取设备的一些配置信息
 */


public class DeviceUtil {

    /**
     * @param context
     * @return 设备ID
     */
    public static String getIMEI(Context context) {
        String imei = null;
        try {
            TelephonyManager tel = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            imei = tel.getDeviceId();
        } catch (Exception e) {
            e.printStackTrace();
            XLog.e(e);
        }
        return null == imei ? "" : imei;
    }

    /**
     * @param context
     * @return 手机IMSI
     */
    public static String getIMSI(Context context) {
        String imsi = null;
        try {
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            imsi = telephonyManager.getSubscriberId();
        } catch (Exception e) {
            e.printStackTrace();
            XLog.e(e);
        }

        return null == imsi ? "" : imsi;
    }

    /**
     * @return phone type ,手机型号,如: Moto,HTC
     */
    public static String getUA() {
        return android.os.Build.MODEL;
    }

    /**
     * @return SDK版本号
     */
    public static int getSDK() {
        return android.os.Build.VERSION.SDK_INT;
    }

    /**
     * @return 手机电话号码
     */
    public static String getPhoneNumber(Context context) {
        String number = null;
        try {
            TelephonyManager tel = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            number = tel.getLine1Number();
        } catch (Exception e) {
            e.printStackTrace();
            XLog.e(e);
        }
        return null == number ? "" : number;
    }

    public static int getCpuCores() {
        try {
            File dir = new File("/sys/devices/system/cpu/");
            File[] files = dir.listFiles(new CpuFilter());
            return files.length;
        } catch (Exception e) {
            e.printStackTrace();
            return 2;
        }
    }

    static class CpuFilter implements FileFilter {
        @Override
        public boolean accept(File pathname) {
            // Check if filename is "cpu", followed by a single digit number
            if (Pattern.matches("cpu[0-9]", pathname.getName())) {
                return true;
            }
            return false;
        }
    }

    /**
     * 获取AndroidId
     *
     * @param context
     * @return
     */
    public static String getAndroidId(Context context) {
        ContentResolver cr = context.getContentResolver();
        String androidid = Settings.System.getString(cr, Settings.Secure.ANDROID_ID);
        if (TextUtils.isEmpty(androidid)) {
            androidid = "unknow";
        }
        return androidid;
    }

    /**
     * 获取屏幕分辨率
     *
     * @param context
     * @return
     */
    public static String getScreenResolution(Context context) {
        Point point = ScreenUtil.getRealSize(context);
        return point.x + "x" + point.y;
    }


}
